package com.totoro.tygeneratedragonblock.manager;

import com.totoro.tygeneratedragonblock.TyGenerateDragonBlock;
import com.totoro.tygeneratedragonblock.data.ChunkCoordinate;
import com.totoro.tygeneratedragonblock.pojo.SpawnPoint;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/10 - 3:29
 * @description
 **/
public class RespawnTasksManager {

    private final TyGenerateDragonBlock plugin;

    @Getter private final Map<Location, BukkitRunnable> respawnTasks = new ConcurrentHashMap<>();

    public RespawnTasksManager(TyGenerateDragonBlock plugin) {
        this.plugin = plugin;
        generateForLoadedChunks();
    }


    /**
     * @author totoro
     * @description 生成所有加载了区块刷新点的方块
     * @date  19:11
     * @param []
     * @return void
     **/
    public void generateForLoadedChunks() {
        // 取消所有重生任务
        cancelAllTask();
        // 生成所有配置的刷新点方块
        long startTime = System.nanoTime();
        int generatedCount = 0;

        // 遍历所有已加载区块
        for (World world : Bukkit.getWorlds()) {
            for (Chunk chunk : world.getLoadedChunks()) {
                ChunkCoordinate chunkCoordinate = new ChunkCoordinate(chunk);
                Set<String> spawnIds = plugin.getChunkMappingManager().getChunkSpawnMap().get(chunkCoordinate);
                if (spawnIds != null) {
                    for (String spawnId : spawnIds) {
                        SpawnPoint spawn = plugin.getSpawnersManager().getSpawnPoints().get(spawnId);
                        if (spawn != null) {
                            // 只生成未激活的方块
                            if (!plugin.getSpawnersManager().getLocationToSpawnId().containsKey(spawn.getLocation())) {
                                plugin.getSpawnersManager().placeSpawnBlock(spawn);
                                generatedCount++;
                            }
                        }
                    }
                }
            }
        }

        long duration = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
        plugin.getLogger().info("已为 " + generatedCount + " 个刷新点生成方块 (" + duration + "ms)");
    }
    
    /**
     * @author totoro
     * @description 取消所有重生任务
     * @date  3:32
     * @param []
     * @return void
     **/
    public void cancelAllTask() {
        for (BukkitRunnable task : respawnTasks.values()) {
            task.cancel();
        }
        respawnTasks.clear();
    }

    /**
     * @author totoro
     * @description 删除某个坐标点的任务
     * @date  3:33
     * @param [location]
     * @return org.bukkit.scheduler.BukkitRunnable
     **/
    public BukkitRunnable remove(Location location) {
        return respawnTasks.remove(location);
    }

    /**
     * @author totoro
     * @description 添加某个坐标点的任务
     * @date  3:34
     * @param [location, task]
     * @return void
     **/
    public void put(Location location, BukkitRunnable task) {
        respawnTasks.put(location, task);
    }
}
